package com.zsxb.crm.controller;

import com.zsxb.crm.annotation.Log;
import com.zsxb.crm.dto.RoleDTO;
import com.zsxb.crm.dto.RolePageDTO;
import com.zsxb.crm.pojo.Role;
import com.zsxb.crm.result.MenuTreeResult;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.result.Result;
import com.zsxb.crm.service.MenuService;
import com.zsxb.crm.service.RoleService;
import com.zsxb.crm.vo.MenuTrees;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 2024/10/28
 */
@RestController
@Slf4j
@RequestMapping("/system")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;
    private final MenuService menuService;

    /**
     * 角色分页查询
     *
     * @param rolePageDTO
     * @return
     */
    @GetMapping("/role/list")
    public PageResult rolePage(RolePageDTO rolePageDTO) {
        log.info("角色分页查询：{}", rolePageDTO);
        PageResult pageResult = roleService.rolePage(rolePageDTO);
        return PageResult.success(pageResult);
    }

    /**
     * 角色菜单树
     *
     * @param id
     * @return
     */
    @GetMapping("/menu/roleMenuTreeselect/{id}")
    public MenuTreeResult roleMenuTreeselect(@PathVariable("id") Long id) {
        List<MenuTrees> menus = menuService.roleMenuTreeselect();
        MenuTreeResult m = new MenuTreeResult();
        m.setMenus(menus);
        //TODO 模拟菜单选中
        m.setCheckedKeys(new ArrayList<>());
        MenuTreeResult menuTreeResult = MenuTreeResult.success(m);
        return menuTreeResult;
    }


    /**
     * 根据角色id查询角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/role/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Role role = roleService.selectById(id);
        return Result.success(role);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/role/{ids}")
    @Log
    public Result delete(@PathVariable("ids") String ids) {
        log.info("删除角色：{}", ids);
        roleService.delete(ids);
        return Result.success();
    }

    /**
     * 修改角色状态
     *
     * @param role
     * @return
     */
    @PutMapping("/role/changeStatus")
    @Log
    public Result changeStatus(@RequestBody Role role) {
        log.info("修改角色状态：{}", role);
        roleService.update(role);
        return Result.success();
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PutMapping("/role")
    @Log
    public Result update(@RequestBody Role role) {
        log.info("修改角色：{}", role);
        roleService.update(role);
        return Result.success();
    }

    /**
     * 修改角色数据权限
     *
     * @param dto
     * @return
     */
    @PutMapping("/role/dataScope")
    @Log
    public Result updateDataScope(@RequestBody RoleDTO dto) {
        log.info("修改角色数据权限：{}", dto);
        roleService.updateDataScope(dto);
        return Result.success();
    }

}
